//
//  ViewController.m
//  SaveDate1
//
//  Created by Justsoso on 16/3/14.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

#import "Student.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#define kStuId @"StuId"
#define KStuName @"StuName"
#define kStuAge @"StuAge"
- (IBAction)save1:(id)sender
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setObject:self.idTextField.text forKey:kStuId];
    [d setObject:self.nameTextField.text forKey:KStuName];
    [d setObject:self.ageTextField.text forKey:kStuAge];
    
    [d synchronize];
}
- (IBAction)read1:(id)sender
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    self.idTextField.text = [d objectForKey:kStuId];
    self.nameTextField.text = [d objectForKey:KStuName];
    self.ageTextField.text = [d objectForKey:kStuAge];
}
- (IBAction)save2:(id)sender
{
    NSDictionary *dict = @{@"stuId":self.idTextField.text,
                           @"stuName":self.nameTextField.text,
                           @"stuAge":self.ageTextField.text};
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    
    [dict writeToFile:filePath atomically:YES];
}
- (IBAction)read2:(id)sender
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.idTextField.text = dict[@"stuId"];
    self.nameTextField.text = dict[@"stuName"];
    self.ageTextField.text = dict[@"stuAge"];
    
}
- (IBAction)save3:(id)sender
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    
    Student *m = [[Student alloc] initWithId:self.idTextField.text
                                     andName:self.nameTextField.text
                                      andAge:self.ageTextField.text];
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:m forKey:@"Student"];
    [arch finishEncoding];
    
    [mData writeToFile:filePath atomically:YES];
    
}
- (IBAction)read3:(id)sender
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    Student *stu = [unArch decodeObjectForKey:@"Student"];
    [unArch finishDecoding];
    
    self.idTextField.text = stu.stuId;
    self.nameTextField.text = stu.stuName;
    self.ageTextField.text = stu.stuAge;
}

@end
